package com.my.mongodbspringdemo.entity;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * CI表单
 * 
 * @author qiyf 2016年4月18日 下午4:06:25
 */

@Document(collection = "ciForm")
@CompoundIndexes(@CompoundIndex(name = "idx_form_tenantId_classCode", def = "{'tenantId':1,'classCode':1}"))
public class CIForm{
	/**
	 * 表单中基本信息分组的编码
	 */
	public static final String CONTAINER_CODE_BASE_INFO_CODE = "baseInfo";
	public static final String CONTAINER_CODE_BASE_INFO_NAME = "基本信息";
	private String id;
	private String tenantId;
	private String classCode;
	private List<CIFormContainer> containers;




	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTenantId() {
		return tenantId;
	}

	public void setTenantId(String tenant) {
		this.tenantId = tenant;
	}

	public String getClassCode() {
		return classCode;
	}

	public void setClassCode(String classCode) {
		this.classCode = classCode;
	}

	public List<CIFormContainer> getContainers() {
		return containers;
	}

	public void setContainers(List<CIFormContainer> containers) {
		this.containers = containers;
	}

	public void setReadOnlyValue(Map<String, Boolean> readOnlyCache, List<String> codes) {
		if (this.containers == null || this.containers.isEmpty())
			return;

		this.containers.stream().forEach(con -> {
			if (codes.contains(con.getCode())) {
				con.setReadOnly(readOnlyCache.get(con.getCode()));
			}
		});
	}

	public boolean getContainerByCode(String code) {
		if (this.containers == null || this.containers.isEmpty())
			return false;

		List<CIFormContainer> cons = this.containers.stream().filter(con -> code.equals(con.getCode())).collect(Collectors.toList());
		if (cons == null || cons.isEmpty())
			return false;
		return cons.get(0).isReadOnly();
	}

	@Override
	public String toString() {
		return "CiForm [id=" + id + ", tenant=" + tenantId + ", classId=" + classCode + ", form=" + containers + "]";
	}
}
